
import 'package:flutter_book/api/search/search_api.dart';
import 'package:flutter_book/data/enum/loading_status.dart';
import 'package:flutter_book/data/model/book_chapter_model.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';

class BookPageController extends GetxController {
  List<BookChapterModel> chapterModelList = [];

  var currentChapterIndex = 0;

  static const String previousString = "上一章";
  static const String currentString = "当前这章";
  static const String nextString = "下一章";

  //上
  var previousStatus = LoadingStatus.STATUS_LOADING;

  //当前
  var currentStatus = LoadingStatus.STATUS_LOADING;

  //下
  var nextStatus = LoadingStatus.STATUS_LOADING;

  init(String bookId, {Function()? initCompleteListener}) async {
    chapterModelList.addAll(await SearchApi.init().getBookCatalogList(bookId));
    if (chapterModelList.length > 0) {
      currentChapterIndex = 0;
      await initChapterContent(currentChapterIndex, currentString);
      await initChapterContent(currentChapterIndex + 1, nextString);
    } else {
      previousStatus = LoadingStatus.STATUS_NO_DATA;
      currentStatus = LoadingStatus.STATUS_NO_DATA;
      nextStatus = LoadingStatus.STATUS_NO_DATA;
      "没有目录".toast;
    }
    if (initCompleteListener != null) {
      initCompleteListener();
    }
  }

  //初始化章节
  initChapterContent(int position, String typePosition) async {
    LoadingStatus loadingStatus = LoadingStatus.STATUS_LOADING;
    if (position >= 0 && position < chapterModelList.length) {
      if (chapterModelList[position].cnContent == null) {
        await SearchApi.init().getBookContent(chapterModelList[position]);
      }
      loadingStatus = LoadingStatus.STATUS_COMPLETED;
    } else {
      loadingStatus = LoadingStatus.STATUS_ERROR;
    }
    switch (typePosition) {
      case previousString:
        previousStatus = loadingStatus;
        update([previousString]);
        break;
      case currentString:
        currentStatus = loadingStatus;
        update([currentString]);
        break;
      case nextString:
        nextStatus = loadingStatus;
        update([nextString]);
        break;
    }
  }

  //向下加载章节
  nextChapterContent() async {
    if (isHaveNextChapter()) {
      clearChapterContent(currentChapterIndex - 1);
      currentChapterIndex++;
      // await initChapterContent(currentChapterIndex - 1, previousString);
      // await initChapterContent(currentChapterIndex, currentString);
      await initChapterContent(currentChapterIndex + 1, nextString);
    } else {
      "没有下一章了：获取数据的地方".print;
    }
  }

  //向上加载章节
  previousChapterContent() async {
    if (isHavePreChapter()) {
      clearChapterContent(currentChapterIndex + 1);
      currentChapterIndex--;
      await initChapterContent(currentChapterIndex - 1, previousString);
      // await initChapterContent(currentChapterIndex, currentString);
      // await initChapterContent(currentChapterIndex + 1, nextString);
    } else {
      "没有上一章了：获取数据的地方".print;
    }
  }

  clearChapterContent(int index) {
    if (index >= 0 && index < chapterModelList.length) {
      chapterModelList[index].cnContent = null;
    }
  }

  //判断还有没有 上一章
  bool isHavePreChapter() {
    return currentChapterIndex > 0;
  }

  //判断还有没有下一章
  bool isHaveNextChapter() {
    return currentChapterIndex < chapterModelList.length - 1;
  }

  //获取上一章
  BookChapterModel? getPreChapter() {
    BookChapterModel? model = _getIndexChapter(currentChapterIndex - 1);
    if (model == null) {
      "获取上一章节内容失败".print;
    }
    return model;
  }

  //获取当前章节内容
  BookChapterModel? getCurrentChapter() {
    BookChapterModel? model = _getIndexChapter(currentChapterIndex);
    if (model == null) {
      "获取当前章节内容失败".print;
    }
    return model;
  }

  //获取下一章内容
  BookChapterModel? getNextChapter() {
    BookChapterModel? model = _getIndexChapter(currentChapterIndex + 1);
    if (model == null) {
      "获取下一章节内容失败".print;
    }
    return model;
  }

  //获取指定章节内容
  BookChapterModel? _getIndexChapter([int? currentChapterIndex]) {
    if (currentChapterIndex == null) {
      currentChapterIndex = this.currentChapterIndex;
    }
    if (currentChapterIndex >= 0 && currentChapterIndex < chapterModelList.length) {
      return chapterModelList[currentChapterIndex];
    }
    return null;
  }

  List<String> getCurrentChapterParagraphs() {
    return getIndexChapterParagraphs(currentChapterIndex);
  }

  List<String> getPreChapterParagraphs() {
    return getIndexChapterParagraphs(currentChapterIndex - 1);
  }

  List<String> getNextChapterParagraphs() {
    return getIndexChapterParagraphs(currentChapterIndex + 1);
  }

  List<String> getIndexChapterParagraphs(int index) {
    BookChapterModel? model = _getIndexChapter(index);
    int _paragraph = 0;
    if (model != null && model.cnContent != null) {
      return model.cnContent!
          .split("\r\n")
          .where((e) => e.isNotEmpty)
          .map((e) => e.toString().trim())
      // .map((e) => "[开始${++_paragraph}]$e[结束$_paragraph]")
          .toList();
    }
    return [];
  }
}
